package com.leyou.task.job;

import net.bytebuddy.agent.builder.AgentBuilder;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.joda.time.DateTime;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import static com.leyou.common.constants.RocketMQConstants.TAGS.SECKILL_ORDER_TAGS;
import static com.leyou.common.constants.RocketMQConstants.TOPIC.SECKILL_TOPIC_NAME;

@Component
public class CreateSeckillPageTask {


    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private RedissonClient redissonClient;

//    @Scheduled(cron = "30 42 11 * * ?")
//    @Scheduled(cron = "0/30 * * * * ?")
    @Scheduled(fixedDelay = 30000) //每个30秒执行一次
    public void createSeckillPage(){
        RLock lock = redissonClient.getLock("seckillPage");
        boolean b = lock.tryLock();
        if(b){
            try {
                String nowDate = DateTime.now().toString("yyyy-MM-dd");
                rocketMQTemplate.convertAndSend(SECKILL_TOPIC_NAME+":"+SECKILL_ORDER_TAGS ,nowDate);
                System.out.println("向mq中发送了一个生成秒杀静态页面的任务");
            } catch (MessagingException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }
        }
    }


}
